package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ApCollectionService;
import com.heima.common.article.ArticleConstans;
import com.heima.common.constans.message.MessageConstants;
import com.heima.model.article.dto.CollectionBehaviorDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.mess.app.UpdateArticleMess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements ApCollectionService {

    @Autowired
    ApArticleService articleService;

    @Autowired
    KafkaTemplate kafkaTemplate;

    /**
     * 文章收藏
     * @param dto
     */
    @Override
    public void collection(CollectionBehaviorDto dto) {
        if (dto==null){
            return;
        }
        if (dto.getOperation()!=ArticleConstans.TYPE_COLLECTION&&dto.getOperation()!=ArticleConstans.TYPE_NO_COLLECTION){
            return;
        }
        ApArticle article = articleService.getById(dto.getEntryId());
        if (article==null){
            return;
        }
        if (dto.getPublishedTime()==null){
            dto.setPublishedTime(article.getPublishTime());
        }

        //查询数据库判断是否存在收藏
        ApCollection collection = getOne(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getArticleId, dto.getEntryId()).eq(ApCollection::getEntryId, dto.getEquipmentId()));

        //根据参数判断是收藏还是取消收藏
        if (dto.getOperation()== ApCollection.Type.ARTICLE.getCode()){
            if (collection==null){
                collection=new ApCollection();
                collection.setCollectionTime(new Date());
                collection.setArticleId(dto.getEntryId());
                collection.setEntryId(dto.getEquipmentId());
                collection.setType(dto.getType());
                collection.setPublishedTime(dto.getPublishedTime());
                save(collection);

                //发送消息进行实时计算
                sendmessage(dto,1);
            }

        }else {
            if (collection!=null){
                removeById(collection.getId());
                sendmessage(dto,-1);
            }
        }

    }

    //发送消息进行实时计算
    private void sendmessage(CollectionBehaviorDto dto,int i) {
        UpdateArticleMess mess=new UpdateArticleMess();
        mess.setAdd(1);
        mess.setArticleId(dto.getEntryId());
        mess.setType(UpdateArticleMess.UpdateArticleType.COLLECTION);
        kafkaTemplate.send(MessageConstants.HOT_ARTICLE_BEHAVIOR_SEND_TOPIC, JSON.toJSONString(mess));
    }
}